import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import { utils } from "../../../../utils/CommonUtil";

enum RotateStatus {
    Oringin,
    Target,
}

export default class RotateObj extends InteractiveBase {
    /** 目标旋转方向*/
    public targetRotate: Laya.Vector3;
    /** 旋转时间*/
    public rotateTime: number = 500;
    /** 初始局部旋转*/
    private originRotate: Laya.Vector3;

    /** 当前的旋转状态*/
    private curRotateStatus: RotateStatus = RotateStatus.Oringin;
    /** 是否只旋转一次*/
    public IsRotateOnce: boolean = false;
    /** 要播放的音频资源名字*/
    public VoiceName: string;

    public isConvertUnityRotation = false;

    private monwer: Laya.Sprite3D;

    private isMoving: boolean;

    onStart() {
        this.monwer = this.owner as Laya.Sprite3D;
        this.originRotate = this.monwer.transform.localRotationEuler.clone();
        this.startByBase();
    }

    protected startByBase(){

    }

    protected InteractiveLogic(): boolean {
        if (this.curRotateStatus == RotateStatus.Oringin) {
            this.MoveTarget();
        }
        else {
            if (!this.IsRotateOnce) {
                this.MoveOrigin();
            }
        }
        if (!utils.CommonUtils.isEmpty(this.VoiceName)) {
            window.gameFacade.SoundMng.playSound(this.VoiceName);
        }
        return true;
    }
    private MoveTarget(): void {
        if(!this.IsRotateOnce){
            this.curRotateStatus = RotateStatus.Target;
        }
        
        this.isMoving = true;
        utils.CommonUtils.DORotateTween(
            this.monwer.transform,
            this.monwer.transform.localRotationEuler,
            this.targetRotate,
            this.rotateTime,
            Laya.Handler.create(this, () => {
                this.isMoving = false;
                let coolider = this.monwer.getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider;
                if (coolider != null && this.IsRotateOnce) {
                    coolider.enabled = false;
                }
                // coolider.colliderShape.localRotation = this.monwer.transform.localRotation;
                // coolider.colliderShape.updateLocalTransformations();
            }),true,this.isConvertUnityRotation
        )
    }

    private MoveOrigin(): void {
        this.curRotateStatus = RotateStatus.Oringin;
        this.isMoving = true;
        utils.CommonUtils.DORotateTween(
            this.monwer.transform,
            this.monwer.transform.localRotationEuler,
            this.originRotate,
            this.rotateTime,
            Laya.Handler.create(this, () => {
                this.isMoving = false;
            }),
            true, false
        )
    }
    /** public 交互逻辑*/
    public Interactive(): void {
        this.InteractiveLogic();
    }
    /** 是否正在移动*/
    public IsMoving(): boolean {
        return this.isMoving;
    }
}